package com.ajk.shop.dao.provider;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import com.ajk.shop.model.OrderDetail;
import com.ajk.shop.model.request.OrderInfoReq;

/** 
 * @author 作者 : chenxuan
 * @version 创建时间：2016年1月6日 下午4:52:57 
 * 类说明 
 */
public class OrderMapperProvider {

	public String batchCreateOrderDetail(Map<String,Object>  map){
		//@Param("orderDetails") List<OrderDetail> orderDetails,@Param("orderId") Integer orderId
		StringBuffer sb = new StringBuffer();
		List<OrderDetail> orderDetails = (List<OrderDetail>)map.get("orderDetails");
		sb.append("insert into order_detail(order_id,goods_id,goods_name,goods_batch_number,sale_price,real_price,goods_number,product_id,is_real,is_service,market_price,type) values");
	    MessageFormat messageFormat = new MessageFormat("(#'{'orderId},#'{'orderDetails[{0}].goodsId},#'{'orderDetails[{0}].goodsName},#'{'orderDetails[{0}].goodsBatchNumber},"
	    		+ "#'{'orderDetails[{0}].salePrice},#'{'orderDetails[{0}].realPrice},#'{'orderDetails[{0}].goodsNumber},"
	    		+ "#'{'orderDetails[{0}].productId},#'{'orderDetails[{0}].isReal},#'{'orderDetails[{0}].isService},#'{'orderDetails[{0}].marketPrice},#'{'orderDetails[{0}].type})");
		int len = orderDetails.size();
		for(int i=0;i<len;i++){
			sb.append(messageFormat.format(new Integer[]{i}));
            sb.append(",");
	    }
	    sb.setLength(sb.length() - 1);
		return sb.toString();
	}
	
	public String userOrderList(OrderInfoReq orderInfoReq){
		StringBuffer sb = new StringBuffer();
		sb.append("select * from (select a.id,a.order_title,a.goods_number,b.order_status_view order_status,a.order_no,a.order_img,a.create_time,a.total_payee,c.sale_price from order_info a ");
		sb.append(" left join order_status_config b ON a.order_status = b.order_status AND a.pay_status = b.pay_status AND ");
		sb.append("a.shipping_status = b.shipping_status left join order_detail c on a.id = c.order_id where user_id = #{userId}) t where t.order_status not in (-2,-3) ");
		if(orderInfoReq.getOrderStatus() != null){
			sb.append(" and t.order_status = #{orderStatus} ");
		}
		sb.append(" order by t.create_time desc ");
		if(orderInfoReq.getStart() != null){
			sb.append(" limit #{start},#{limit}");
		}
		return sb.toString();
	}
	
	public String orderList(OrderInfoReq orderInfoReq){
		StringBuffer sb = new StringBuffer();
		sb.append("select * from (select a.id,a.user_id,a.order_title,a.goods_number,b.order_status_view order_status,a.order_no,a.order_img,a.create_time,a.total_payee,c.sale_price,a.shipping_payee from order_info a ");
		sb.append(" left join order_status_config b ON a.order_status = b.order_status AND a.pay_status = b.pay_status AND ");
		sb.append("a.shipping_status = b.shipping_status left join order_detail c on a.id = c.order_id ");
		if(orderInfoReq.getUserId()!=null && orderInfoReq.getUserId()>0){
			 sb.append(" where user_id = #{userId}");
		}
		sb.append(") t where t.id>0 ");
		if(orderInfoReq.getOrderStatus() != null){
			sb.append(" and t.order_status = #{orderStatus} ");
		}
		sb.append(" order by t.create_time desc ");
		if(orderInfoReq.getStart() != null){
			sb.append(" limit #{start},#{limit}");
		}
		return sb.toString();
	}
	
	public String orderListCount(OrderInfoReq orderInfoReq){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(1) from (select a.id,a.user_id,a.order_title,a.goods_number,b.order_status_view order_status,a.order_no,a.order_img,a.create_time,a.total_payee,c.sale_price,a.shipping_payee from order_info a ");
		sb.append(" left join order_status_config b ON a.order_status = b.order_status AND a.pay_status = b.pay_status AND ");
		sb.append("a.shipping_status = b.shipping_status left join order_detail c on a.id = c.order_id ");
		if(orderInfoReq.getUserId()!=null && orderInfoReq.getUserId()>0){
			 sb.append(" where user_id = #{userId}");
		}
		sb.append(") t where t.id>0 ");
		if(orderInfoReq.getOrderStatus() != null){
			sb.append(" and t.order_status = #{orderStatus} ");
		}
		return sb.toString();
	}

}
